package com.fyl.aop;

import org.springframework.aop.ClassFilter;
import org.springframework.aop.MethodMatcher;
import org.springframework.aop.Pointcut;
import org.springframework.aop.support.ClassFilters;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

public class AbstractControllerMethodPointcut implements Pointcut {

    private MethodMatcher methodMatcher;

    public AbstractControllerMethodPointcut(MethodMatcher methodMatcher) {
        this.methodMatcher = methodMatcher;
    }

    @Override
    public ClassFilter getClassFilter() {
        return ClassFilters.union(clazz -> clazz.isAnnotationPresent(Controller.class),
                clazz -> clazz.isAnnotationPresent(RestController.class));
    }

    @Override
    public MethodMatcher getMethodMatcher() {
        return this.methodMatcher;
    }

    public static AbstractControllerMethodPointcut forRequestMappingMethodMatcher() {
        return new AbstractControllerMethodPointcut(new RequestMappingMethodMatcher());
    }

    public static AbstractControllerMethodPointcut forMethodMatcher(MethodMatcher methodMatcher) {
        return new AbstractControllerMethodPointcut(methodMatcher);
    }
}
